//
//  CityVC.m
//  违章查询助手
//
//  Created by qianfeng on 15/10/26.
//  Copyright (c) 2015年 LY. All rights reserved.
//

#import "CityVC.h"
#import "ChineseToPinyin.h"
#import "AddDriveVC.h"
#import "LiycRequest.h"
#import "MBProgressHUD.h"
#import "TableViewShow.h"

int _myitem;
NSInteger _row;
NSInteger _line;

@interface CityVC ()<UITableViewDataSource,UITableViewDelegate,TableViewShowDelegate>
{
    TableViewShow * viewShow;
}
@end

@implementation CityVC
{
    UITableView * _tableView;
    NSMutableArray * arr;
    NSMutableArray * array;
    NSMutableArray * _dataArray;
    NSMutableArray * citynameArray;
 
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self creatNavBar];
    [self creatNav];
    
    self.title = @"选择城市";
    arr = [NSMutableArray array];
    citynameArray = [NSMutableArray array];
    for (int i=65; i<91; i++) {
        NSString * string = [NSString stringWithFormat:@"%C",(unichar)i];
        [arr addObject:string];
    }
    
    [self getData];
    [self createTableView];
}

//数据获取
-(void)getData
{
    [LiycRequest getAddressWithBlock:^(id obj, NSError *error) {
        if (!error) {
            NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:obj options:NSJSONReadingMutableContainers error:nil];
            NSDictionary * result = dic[@"result"];
            NSArray * items = result[@"items"];
            _dataArray = [NSMutableArray array];
            
            for (int i=0; i<26; i++) {
                array = [NSMutableArray array];
                for (NSDictionary * item in items) {
                    addressModel * model = [[addressModel alloc]initWithDict:item];
                    if ([model.fl isEqualToString:arr[i]]) {
                        [array addObject:model];
                    }
                }
                [_dataArray addObject:array];
            }
            for (int i=0; i<_dataArray.count; i++) {
                if ([_dataArray[i] count] == 0) {
                    [_dataArray removeObject:_dataArray[i]];
                }
            }
            for (int i = 0; i<_dataArray.count; i++) {
                addressModel * model = _dataArray[i][0];
                NSString * str = model.fl;
                [citynameArray addObject:str];
            }
            
            [_tableView reloadData];
            [MBProgressHUD hideHUDForView:self.view animated:YES];

        }else
        {
            _dataArray=[NSMutableArray array];
            [_tableView reloadData];
            NSLog(@"%@",error);
        }
        
    }];
    }

//创建TableView
-(void)createTableView
{
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, Width, Height-64) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_dataArray[section] count];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//    if (_dataArray.count==0) {
//        
//        return 20;
//    }
    return _dataArray.count;
}

-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return citynameArray[section];
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = [_tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    addressModel * model = _dataArray[indexPath.section][indexPath.row];
    cell.textLabel.text = model.pn;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //_row = indexPath.section;
    NSInteger lll = 0;
    for (int i=0; i<indexPath.section; i++) {
        lll += [_dataArray[i] count];
    }
    lll += indexPath.row;
    _row = lll;
    NSLog(@"_row === %lu",_row);
    
    [self createShowView];
}

//弹出视图,获取城市
-(void)createShowView
{
    viewShow = [[TableViewShow alloc]initWithFrame:CGRectMake(100, 64, 300, HEIGHT-64)];
    viewShow.delegate = self;
    [viewShow looklookView];
}

-(void)viewDisappear:(NSString *)carType andId:(NSNumber *)cid
{
    
    NSLog(@"city = %@",carType);
    _city = carType;
    

    [self.delegate sendString:carType andId:cid];
    [viewShow disappearView];

    [self dismissViewControllerAnimated:YES completion:nil];
    
}

//创建导航栏
-(void)creatNav
{
    UINavigationBar * bar=self.navigationController.navigationBar;
    [bar setBackgroundImage:[UIImage imageNamed:@"custom_dialog_btn_bg"] forBarMetrics:UIBarMetricsDefault];
    [self.view addSubview:bar];
    
}

-(void)creatNavBar
{
    UIButton * left=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];
    
    [left setBackgroundImage:[UIImage imageNamed:@"back_normal"] forState:UIControlStateNormal];
    [self.view addSubview:left];
    [left addTarget:self action:@selector(leftButton) forControlEvents:UIControlEventTouchUpInside];
    
    
    UIBarButtonItem * leftItem=[[UIBarButtonItem alloc]initWithCustomView:left];
    self.navigationItem.leftBarButtonItem=leftItem;
}

-(void)leftButton
{
    [self dismissViewControllerAnimated:YES completion:nil];
    //[self.navigationController popToRootViewControllerAnimated:YES];
    [self dismissViewControllerAnimated:YES completion:nil];
    [self dismissViewControllerAnimated:YES completion:nil];
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
